package com.joe.codemother.service.impl;

import com.joe.codemother.exception.BusinessException;
import com.joe.codemother.exception.ErrorCode;
import com.joe.codemother.mapper.UserMapper;
import com.joe.codemother.service.ValidateService;
import com.joe.codemother.util.ThrowUtils;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ValidateServiceImpl implements ValidateService {

    @Resource
    UserMapper userMapper;

    @Override
    public boolean validateRegister(String username, String password, String confirmPassword) {
        boolean anyEmpty = StringUtils.isAnyEmpty(username, password, confirmPassword);
        ThrowUtils.throwIf(anyEmpty, new BusinessException(ErrorCode.PARAMS_ERROR,"The username and password are mandatory"));
        ThrowUtils.throwIf(!password.equals(confirmPassword),new BusinessException(ErrorCode.PARAMS_ERROR,"The passwords do not match"));

        //validate repeat account
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("userAccount",username);
        long result = userMapper.selectCountByQuery(wrapper);
        ThrowUtils.throwIf(result > 0, new BusinessException(ErrorCode.PARAMS_ERROR,"The account has been occupied"));

        return true;
    }

}
